package top.lshaci.learning.activiti.day2;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;

import static top.lshaci.learning.activiti.util.ProcessEngineSingleton.singleton;

/**
 * ExclusiveGatewayTest
 * <p>排他网关测试</p>
 * <pre>
 *     1、排他网关的默认出口，不能添加条件
 * </pre>
 *
 * @author lshaci
 * @since 1.0.0
 */
public class ExclusiveGatewayTest {

    @Test
    public void test() {
        // 1.部署流程
        Deployment deployment = singleton.deploy("ExclusiveGateway");
        System.err.println(deployment);
        // 2.启动流程
        ProcessInstance processInstance = singleton.start("ExclusiveGateway");
        String processInstanceId = processInstance.getId();
        System.err.println("processInstanceId: " + processInstanceId);
        // 3.验证流程的状态（位置）
        // 获取当前流程 "汇总当日销售额" 节点对应的Execution对象
        Execution e1 = singleton.runtimeService()
                .createExecutionQuery()
                .processInstanceId(processInstanceId)
                .activityId("汇总当日销售额ID")
                .singleResult();
        System.err.println("e1: " + e1);
        Assert.assertNotNull(e1);
        // 做了一大堆逻辑，汇总当日销售额
        BigDecimal amount = new BigDecimal("1888.88");
        // 把当日销售额，放入（全局）流程变量
        singleton.runtimeService().setVariable(e1.getId(), "amount", amount);

        // 完成任务，推动流程到下一个节点（activiti5为signal）
        singleton.runtimeService().trigger(e1.getId());

        // 获取当前流程 "给老板发短信/邮件" 节点对应的Execution对象
        Execution e2 = singleton.runtimeService()
                .createExecutionQuery()
                .processInstanceId(processInstanceId)
                .activityId(new BigDecimal("10000").compareTo(amount) > 0 ? "给老板发短信ID" : "给老板发邮件ID")
                .singleResult();
        System.err.println("e2: " + e2);
        Assert.assertNotNull(e2);

        Object amount1 = singleton.runtimeService().getVariable(e2.getId(), "amount");
        System.err.println("今天赚了：￥" + amount1);
        // 完成任务，推动流程到下一个节点
        singleton.runtimeService().trigger(e2.getId());

        // 验证流程结束
        ProcessInstance dbProcessInstance = singleton.runtimeService()
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        Assert.assertNull(dbProcessInstance);
    }
}
